<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MFileMetas extends MModel {
    
    public static function createFileMeta($file_path, $meta_key, $meta_value) {
        $file_meta = new MFileMetas();
        $file_meta->file_path = $file_path;
        $file_meta->meta_key = $meta_key;
        $file_meta->meta_value = $meta_value;
        $meta_value = mysql_escape_string($meta_value);
        $sql = "INSERT INTO " . DB_PREFIX . "_file_metas";
        $sql .= "(file_path,meta_key,meta_value,created_at,updated_at)";
        $sql .= " VALUES(";
        $sql .= "\"$file_path\",'$meta_key','$meta_value',now(),now()";
        $sql .= ")";
        FXP::trace("function: '{createFileMeta}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        if ($db_manager->insertDb($sql) === false){
            return false;
        }
        return $file_meta;
    }
    
    
    public static function updateFileMeta($file_path,$meta_key, $meta_value) {
        $meta_value = mysql_escape_string($meta_value);
        $sql  = "UPDATE " . DB_PREFIX . "_file_metas ";
        $sql .= "SET meta_value = '$meta_value', updated_at = now() ";
        $sql .= "WHERE file_path=\"$file_path\" AND meta_key=\"$meta_key\"";
        FXP::trace("function: '{updateFileMeta}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        return $db_manager->updateDb($sql);
    }
    
    
    public static function queryFileMeta($file_path,$meta_key) {
        $sql = "SELECT * FROM  " . DB_PREFIX . "_file_metas ";
        $sql .= "WHERE file_path=\"$file_path\" AND meta_key=\"$meta_key\"";
        FXP::trace("function: '{queryFileMeta}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        return $db_manager->selectDb($sql);
    }
    
    
    public static function batchQueryFileMeta($meta_key, $file_array)
    {
        if (empty($file_array))
        {
            return $file_array;
        }
        $sql = "SELECT * FROM  " . DB_PREFIX . "_file_metas ";
        $sql .= " where meta_key='$meta_key' AND file_path in (";
        foreach ($file_array as $file)
        {
            $sql .= "\"{$file->file_path}\",";
        }
                                $sql = substr($sql, 0, -1);
        $sql .= ")";
        FXP::trace("function: '{queryFileMeta}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        return $db_manager->selectDb($sql);
    }
    
    
    public static function batchCreateFileMetas(
        $create_array,
        $meta_key
        )
    {
        $sql  = "insert into ".DB_PREFIX."_file_metas(file_path,meta_key,";
        $sql .= "meta_value,created_at,updated_at) values ";
        $is_add = false;
        foreach ($create_array as $file_meta)
        
        {
            if ($file_meta->is_add === false)
            {
                                continue;
            }
            $is_add = true;
            $sql .= "(\"{$file_meta->file_path}\",";
            $sql .= "\"{$meta_key}\",";
            $sql .= "'{$file_meta->meta_value}',";
            $sql .= "now(),now()),";
        }
        if ($is_add === false)
        {
                        return true;
        }

                                $sql = substr($sql, 0, -1);
        FXP::trace("function: '{batchCreateFileMetas}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        return $db_manager->insertDb($sql);
    }
}

?>